package com.itheima.controllerweb;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {

    public final static String ALL_IMG_KEY = "setmealCache";

    @Autowired
    private SetmealService setmealService;

    @Autowired
    public  static String USED_IMG_KEY = "setmealUsedCache";//图片

    @Autowired
    private AliOssUtil aliOssUtil;//图片上传时用到

    @Autowired
    private RedisTemplate<String, String> redisTemplate;//图片

    //套餐管理 分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setmealService.findPage(queryPageBean);
        log.info("分页查询结果：{}",pageResult);
        return pageResult;
    }

    //套餐管理 新增套餐
    @RequestMapping("/add")
    public Result save(@RequestBody Setmeal setmeal,
                       @RequestParam("checkgroupIds") List<Integer> checkGroupIds){
        log.info("新增套餐：{}",setmeal,checkGroupIds);
        setmealService.add(setmeal,checkGroupIds);
        return new Result(true,"新增成功");
    }

    //套餐管理 修改回显
    @GetMapping("/findById")
    public Result findById(@RequestParam("id") Integer id){
        log.info("修改回显：{}",id);
        Setmeal setmeal = setmealService.findById(id);
        return new Result(true,"查询成功",setmeal);
    }

    //套餐管理 修改套餐
    @PostMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal,
                       @RequestParam("checkgroupIds") List<Integer> checkGroupIds){
        log.info("修改套餐：{}",setmeal,checkGroupIds);
        setmealService.update(setmeal,checkGroupIds);
        return new Result(true,"修改成功");
    }

    //套餐管理 删除套餐
    @GetMapping("/deleteById")
    public Result deleteById(@RequestParam("id") Integer id){
        log.info("删除套餐：{}",id);
        setmealService.delete(id);
        return new Result(true,"删除成功");
    }

    //图片上传
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile) {
        log.info("执行 文件上传 接口:{}", imgFile);
        //获取原始文件名
        String originalFileName = imgFile.getOriginalFilename();
        //断言， 如果名称为null则抛出异常
        assert originalFileName != null;
        //获取后缀最后一次出现的地方
        int last = originalFileName.lastIndexOf(".");
        //获取文件名后缀
        String extension = originalFileName.substring(last);
        //构建新的文件名
        String uuid = UUID.randomUUID().toString();
        //组合新的文件名
        String fileName = uuid + extension;
        //上传文件,并获取文件访问链接
        try {
            String filePath = aliOssUtil.upload(imgFile.getBytes(), fileName);
            //将文件名保存在redis中
            redisTemplate.opsForSet().add(ALL_IMG_KEY, fileName);

            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, filePath);
        } catch (Exception e) {
            log.error(e.toString());
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

}
